package com.demo.dao.impl;

import com.demo.dao.CourseDao;
import com.demo.dao.DepartmentDao;
import com.demo.entity.Course;
import com.demo.entity.Department;
import com.demo.util.Util;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// TODO: 2023/11/12
public class DepartmentDaoImpl implements DepartmentDao {

    public void add(Department vo) {
        //id时自增的不用加
        String sql = "insert into `department` (`depId`,`depName`) values(?,?)";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);

            ps.setLong(1, vo.getDepId());
            ps.setString(2, vo.getDepName());
            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@Override
    public void update(Department vo) {

        String sql = "update `department` set `depName` = ? where `depId` = ?";
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps = c.prepareStatement(sql);

            //这不就是三个值吗
            // TODO: 2023/11/11 先 get 再 set
//            ps.setLong(1, vo.getDepId());
            ps.setString(1, vo.getDepName());
            ps.setLong(2, vo.getDepId());

            ps.execute();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //@Override
    public boolean delete(long depId) {
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "delete from `department` where depId = " + depId;
            s.execute(sql);
            s.close();
            c.close();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    //@Override
    public Department get(Serializable depId) {
        // TODO: 2023/11/11
        Department vo = null;
        try {
            Connection c = Util.getConnection();
            Statement s = c.createStatement();
            String sql = "select * from `department` where depId = " + depId;
            ResultSet rs = s.executeQuery(sql);
            if (rs.next()) {
                vo = new Department();
                // TODO: 2023/11/11 自己打个标记要改哪里
                vo.setDepId(rs.getLong("depId"));
                vo.setDepName(rs.getString("depName"));

            }
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vo;
    }

    //@Override
    public Map<String, Object> list(Map<String, Object> params) {
        // TODO: 2023/11/11
        List<Department> list = new ArrayList();

        int totalCount = 0;
        String condition = "";
        String sqlList;
        if (params.get("searchColumn") != null && !"".equals(params.get("searchColumn"))) {
            condition += " and `" + params.get("searchColumn") + "` like '%" + params.get("keyword") + "%'";
        }
        try {
            Connection c = Util.getConnection();
            PreparedStatement ps;
            ResultSet rs;
            String limit = (params.get("startIndex") != null && params.get("pageSize") != null) ? " limit " + params.get("startIndex") + "," + params.get("pageSize") : "";
            sqlList = "select * from `department` where 1=1 " + condition + " order by depId asc " + limit + ";";
            ps = c.prepareStatement(sqlList);
            rs = ps.executeQuery();
            while (rs.next()) {

                // TODO: 2023/11/11
                Department vo = new Department();
                vo.setDepId(rs.getLong("depId"));

                vo.setDepName(rs.getString("depName"));


                list.add(vo);
            }
            // TODO: 2023/11/11  没有//
            String sqlCount = "select count(*) from `department` where 1=1 " + condition;
            ps = c.prepareStatement(sqlCount);
            rs = ps.executeQuery();
            if (rs.next()) {
                totalCount = rs.getInt(1);
            }
            rs.close();
            ps.close();
            c.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map<String, Object> result = new HashMap();
        result.put("list", list);
        result.put("totalCount", totalCount);
        return result;
    }
}
